﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace PreluareDateDePeEBay
{

    public class ParsareSursa
    {
        public int aparitie;        
        string cifre = "0123456789";
        string rezultat;
       
        public string Gaseste(string url, int Optiune)
        {
            DateTime dtPars1 = DateTime.Now;
            if (Optiune == 1)
            {
                // Sectiune ISBN
                HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                myWebRequest.Method = "GET";                
                myWebRequest.Timeout = 200000;
                myWebRequest.KeepAlive = false;
                myWebRequest.Headers.Add("Cache-Control", "no-cache");
                myWebRequest.Headers.Add("Pragma", "no-cache");
                myWebRequest.ServicePoint.ConnectionLeaseTimeout = 5000;
                myWebRequest.ServicePoint.MaxIdleTime = 5000;

                try
                {
                    DateTime d1 = DateTime.Now;
                    string myPageSource;
                    using (HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse())
                    {
                        StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());
                        myPageSource = myWebSource.ReadToEnd();
                        aparitie = myPageSource.IndexOf("ISBN-10");
                        if (aparitie < 0) return "";
                        aparitie += 5;
                        myWebResponse.Close();
                    }
                    DateTime d2 = DateTime.Now;
                    eISBN(myPageSource,aparitie);
                    rezultat = myPageSource.Substring(aparitie, 10);
                    DateTime d3 = DateTime.Now;
                }
                catch (WebException ex)
                {
                    // asta e
                }
                finally
                {
                    myWebRequest.Abort();
                }

                return rezultat;
            }
            else
            {
                // Sectiune UPC
                HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                myWebRequest.Method = "GET";
                myWebRequest.Timeout = 200000;
                myWebRequest.KeepAlive = false;
                myWebRequest.Headers.Add("Cache-Control", "no-cache");
                myWebRequest.Headers.Add("Pragma", "no-cache");
                myWebRequest.ServicePoint.ConnectionLeaseTimeout = 5000;
                myWebRequest.ServicePoint.MaxIdleTime = 5000;

                try
                {
                    string myPageSource;
                    using (HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse())
                    {
                        StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());
                        myPageSource = myWebSource.ReadToEnd();
                        aparitie = myPageSource.IndexOf("UPC");
                        if (aparitie < 0) return "";
                        aparitie += 3;
                        myWebResponse.Close();
                    }

                    eUPC(myPageSource);
                    rezultat = myPageSource.Substring(aparitie, 12);    
                }
                catch (WebException ex)
                {
                    // asta e
                }
                finally
                {
                    myWebRequest.Abort();
                }

                DateTime dtPars2 = DateTime.Now;
                return rezultat;               
            }
        }

        bool eISBN(string myPageSource,int inceput)
        {
            try
            {
                bool ok; // PP ca gasesc ISBN
                do
                {
                    ok = true;
                    for (int i = 0; i < 10 && ok; i++)
                    {
                        if (!cifre.Contains(myPageSource[aparitie + i]))
                        {
                            ok = false;
                        }
                    }
                    aparitie++;
                } while (ok == false && aparitie + 10 < inceput+200);

                aparitie--;
                return ok;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        bool eUPC(string myPageSource)
        {
            try
            {
                bool ok; // PP ca gasesc UPC
                do
                {
                    ok = true;
                    for (int i = 0; i < 12 && ok; i++)
                    {
                        if (!cifre.Contains(myPageSource[aparitie + i]))
                        {
                            ok = false;
                        }
                    }
                    aparitie++;
                } while (ok == false && aparitie + 12 < myPageSource.Length);

                aparitie--;
                return ok;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
